package tk.mybatis.springboot.controller;

import com.jier.annotation.ValidateField;
import com.jier.annotation.ValidateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.springboot.common.Result;
import tk.mybatis.springboot.common.base.BaseController;
import tk.mybatis.springboot.common.message.ResponseData;
import tk.mybatis.springboot.model.City;
import tk.mybatis.springboot.service.ICityService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/cities")
public class CityController extends BaseController {

    @Autowired
    private ICityService cityServiceImpl;

    @RequestMapping(produces = "application/json;")
    public List<City> getAll(City city) throws Exception {
        List<City> countryList = cityServiceImpl.getAll();
//        return toSuccess(super.resource.getLoad_successful(), countryList);
        return countryList;
    }

    @RequestMapping(value = "/record")
    @ValidateGroup(fields = {
            @ValidateField(index = 0, paramName = "搜索值", notNull = true, maxLen = 6)
    })
    public ResponseData getRecordPage(String keyword, HttpServletResponse response) throws Exception {
        Result<City> countryList = cityServiceImpl.getRecordPage();
        return toSuccess(super.resource.getLoad_successful(), countryList);
    }

    @RequestMapping(value = "/add")
    public City add() {
        return new City();
    }

    @GetMapping(value = "/{id}", produces = "application/json;")
    public ResponseData view(@PathVariable String id) throws Exception {
        City city = cityServiceImpl.getById(id);
        return toSuccess(super.resource.getLoad_successful(), city);
    }

    @PatchMapping(value = "/{id}", produces = "application/json;")
    @ValidateGroup(fields = {
            @ValidateField(index = 1, paramName = "城市名称", notNull = true, maxLen = 15)
    })
    public ResponseData update(@PathVariable String id, String name) throws Exception {
        City city = cityServiceImpl.getById(id);
        if (city == null) {
            return toError(super.resource.getUpdate_error());
        }
        city.setName(name);
        cityServiceImpl.updateById(city);
        return toSuccess(super.resource.getUpdate_successful(), city);
    }

    @RequestMapping(value = "/delete/{id}")
    public ModelMap delete(@PathVariable Integer id) throws Exception {
        ModelMap result = new ModelMap();
        cityServiceImpl.deleteById(id);
        result.put("msg", "删除成功!");
        return result;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelMap save(City city) throws Exception {
        ModelMap result = new ModelMap();
        String msg = city.getId() == null ? "新增成功!" : "更新成功!";
        cityServiceImpl.save(city);
        result.put("city", city);
        result.put("msg", msg);
        return result;
    }
}
